<template>
  <div class="app-container">
    <!--条件筛选区-->
<!--    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="70px" class="condition_form">-->

<!--      <el-row >-->
<!--        <el-col :span="20">-->
<!--          &lt;!&ndash;根据分类名称模糊查询&ndash;&gt;-->
<!--          <el-form-item label="分类名" prop="name">-->
<!--            <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable-->
<!--                      @keyup.enter.native="handleQuery"/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="4">-->
<!--          <el-form-item class="serch-button">-->
<!--            <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>-->
<!--            <el-button icon="el-icon-refresh"  @click="reset">重置</el-button>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
<!--    </el-form>-->
    <!--一级分类操作按钮-->
    <el-button class="serch-button" type="success"  @click="handleAdd('parent')" plain>新增一级分类</el-button>

    <!--activeName:默认显示的第一个的key-->
    <el-tabs v-model="activeName" style="margin-top:60px;" type="border-card">
      <el-tab-pane v-for="(item,index) in list" :key="item.name" :label="item.name" :name="item.name" >
        <el-button class="serch-button" type="success" plain style="margin-bottom: 20px;margin-left: 20px" @click="handleAdd('children')" >新增二级分类</el-button>
        <el-button class="serch-button" type="danger" plain style="margin-bottom: 20px;margin-left: 20px" @click="handleDelete(item)">删除一级分类</el-button>
        <el-button class="serch-button" type="primary" plain style="margin-bottom: 20px;margin-left: 20px" @click="handleUpdate('parent',item)">编辑一级分类</el-button>
        <!--二级分类-->
          <el-table
            :data="item.children"
            border
            stripe
            style="width: 100%;">
            <el-table-column type="selection" width="40" align="center"/>
            <el-table-column
              align="center"
              prop="id"
              label="编号"
              width="180">
            </el-table-column>
            <el-table-column
              align="center"
              prop="name"
              label="分类名称"
              width="180">
            </el-table-column>
            <el-table-column
              align="center"
              prop="description"
              label="分类描述">
            </el-table-column>

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <!--编辑二级分类-->
                <el-button type="text" icon="el-icon-edit" @click="handleUpdate('children',scope.row)">编辑</el-button>
                <!--单个删除二级分类-->
                <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
      </el-tab-pane>
    </el-tabs>

    <!--页数-->
<!--    <el-pagination :page-size.sync="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"-->
<!--                   :total="total" :page-sizes="[5,10, 20, 30, 40]" :current-page.sync="queryParams.pageNum"-->
<!--                   @current-change="getList"-->
<!--                   @size-change="getList"/>-->

    <!-- 添加或修改一级分类弹出层 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="父分类" v-if="parentIsOpen">
          <el-select v-model="form.parentId" placeholder="请选择父分类">
            <el-option v-for="item in list" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {getPageCategory,deleteCategory,updateCategory,addCategory,getCategoryById} from "@/api/category";

export default {
  name: 'Tab',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 父分类是否显示
      parentIsOpen:false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      //分类集合
      list: null,
      //找到第一个父分类名称
      activeName: "",
      // 查询参数
      queryParams: {
        //当前页数
        pageNum: 1,
        //每页大小
        pageSize: 1000,
        //分类名称
        name: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList(){
      getPageCategory(this.queryParams).then((res)=>{
        this.list = res.data.categoryList;
        this.total = res.data.total;
        //找到list集合第一个元素的名称赋值给activeName
        this.activeName = this.list[0].name
      })
    },

    /**
     * 取消按钮
     */
    cancel() {
      this.open = false
      this.reset()
    },

    /**
     * 表单重置
     */
    reset() {
      this.queryParams = {
        //当前页数
        pageNum: 1,
        //每页大小
        pageSize: 10,
        //用户名
        username: ''
      }
      this.form = {}
      //this.getList()
    },

    /**
     * 搜索按钮操作
     */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /**
     * 重置按钮操作
     */
    resetQuery() {
      this.handleQuery()
    },
    /**
     * 多选框选中数据
     * @param selection
     */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /**
     * 新增分类
     */
    handleAdd(type){
      if(type === 'children'){
        this.parentIsOpen=true
        //表单标题
        this.title = '添加二级分类'
      }else{
        this.parentIsOpen=false
        //表单标题
        this.title = '添加分类'
      }
      //表单重置
      this.reset();
      //打开表单
      this.open = true;
    },
    /**
     * 修改按钮操作
     * @param type
     * @param row
     */
    handleUpdate(type,row) {
      if(type === 'children'){
        this.parentIsOpen=true
        //表单标题
        this.title = '修改二级分类'
      }else{
        this.parentIsOpen=false
        //表单标题
        this.title = '修改分类'
      }
      this.reset()
      this.form = {...row};
      this.open = true
    },
    /**
     * 提交按钮
     */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCategory(this.form).then((response) => {
              this.$notify({
                title: '操作成功',
                message: '修改分类成功！',
                type: 'success'
              });
              this.open = false
              this.getList()
            })
          } else {
            addCategory(this.form).then((response) => {
              this.$notify({
                title: '操作成功',
                message: '新增分类成功！',
                type: 'success'
              });
              this.open = false
              location.reload();
              //this.getList()
            })
          }
        }
      })
    },

    /**
     * 删除按钮操作
     * @param row
     */
    handleDelete(row) {
      this.$alert('是否确认删除分类编号为"' + row.id + '"的数据项？', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          return deleteCategory(row.id).then(() => {
            this.getList()
            this.$notify({
              title: '操作成功',
              message: '删除分类成功！',
              type: 'success'
            });
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.condition_form .el-form-item__label {
  font-size: 14px;
}

.app-container {
  margin-left: 20px;
  margin-top: 20px;
}
</style>
